window.onload = function(){
	//监听url变化
	var commonIndex = 'platform-commonGroup-common';
	var url;
	window.addEventListener('hashchange', function(){
		currentUrl = location.hash.slice(1) || commonIndex;
        if(currentUrl.indexOf("cf")!=-1){
            url="/"+currentUrl;
		}else {
            url = contextPath + '/system/ajaxRequestHTML/'+currentUrl+"/main";
		}
		$.ajax({
			type : 'get',
			async : true,
			url : url,
			cache : false,
			success : function(data) {
				$("#content").html(data);
			},
			error: function (data) {
				console.log(data);
			}
		});
	}, false);
	var currentUrl = location.hash.slice(1) || commonIndex;
    if(currentUrl.indexOf("cf")!=-1){
        url="/"+currentUrl;
    }else {
        url = contextPath + '/system/ajaxRequestHTML/'+currentUrl+"/main";
    }
	$.ajax({
		type : 'get',
		async : true,
		url : url,
		cache : false,
		success : function(data) {
			$("#content").html(data);
		},
		error: function (data) {
			console.log(data);
		}
	});
	//主题切换
	$("#themeMenu").on('click', 'li:not(.cur) ',function () {
		var $this = $(this),
			thisID = $this.attr("data-id");
		$this.addClass('cur').siblings().removeClass('cur');
		var $theme = $("#themeLink");
		$theme.attr("href", contextPath + "/css/theme/" + thisID + ".css");
		localStorage.theme = thisID;
	});
	// 侧边栏切换
	$("#asidebarSwitch").on('click',function () {
//	    if($(this).children("i").hasClass("fa-indent")){
//	        $(this).children("i").removeClass("fa-indent").addClass('fa-outdent');
//	    }else if($(this).children("i").hasClass("fa-outdent")){
//	        $(this).children("i").removeClass("fa-outdent").addClass('fa-indent');
//	    }
	    $("#asidebar").toggleClass("asidebar-folded");
	    $("#content").toggleClass("asidebar-folded");
	});
	//生成侧边栏
	$.ajax({
		url: contextPath + "/system/getNavItems",
		type: "get",
		cache: false,
		dataType: "json",
		success: function(data){
			var _str = "",
				_children = "";
			for(var i = 0; i < data.length; i++){
				_str += '<li>'+
			                '<a href="javascript:;">'+
				                '<span class="triangle">'+
				                    '<i class="fa fa-angle-right"></i>'+
				                    '<i class="fa fa-angle-down text-active"></i>'+
				                '</span>'+
				                '<i class="iconfont">'+ data[i].itemIcon +'</i>'+
				                '<span">'+ data[i].name +'</span>'+
				            '</a>'+
				            '<ul class="nav nav-sub">';
				_children = data[i].children;
				for(var j = 0; j < _children.length; j++){
					_str += '<li>'+
                                '<a href="#' + _children[j].pageUrl +'"><span>'+ _children[j].name +'</span></a>'+
                            '</li>';
				}
				_str +='</ul></li>';
			}
			$("#nav").html(_str);
			$(".navList").on("click","#nav>li",function(){
				$(this).find(".nav-sub").slideDown(100);
				$(this).siblings().find(".nav-sub").slideUp(100);
			});
		},
		error: function(data){
			console.log(data);
			$("ul.nav").html("加载出错，请检查您的网络!");
		}
	});
	//登录超时
	var ws = null;
	$.ajax({
		url: contextPath + "/getCurrentSesionId",
		type: "get",
		cache: false,
		dataType: "json",
		success: function(d){
			if(d.code = "1"){
				ws = new WebSocket("ws://localhost:8080" + contextPath + "/websock");
				ws.onopen = function(){
			       // Web Socket 已连接上，使用 send() 方法发送数据
					ws.send(d.message);
					console.log("WebSocket连接成功!");
			    };
			    ws.onmessage = function (event){ 
			       console.log(event);
		    	};
				ws.error = function(event){
					console.log("WebSocket连接异常!" + event);
				}
				ws.onclose = function(event) {
					console.log(event.code);
					if(event.code == 1008){
						$alert("连接超时!", "请重新登录!", function () {
			            	location.reload(true);
			            }, {type: 'info', confirmButtonText: '刷新'}, function () {
			            	location.reload(true);
			            });
					}
				};
			}
		},
		error: function(data){
			console.log(data);
			//$("ul.nav").html("加载出错，请检查您的网络!");
		}
	});
	
	
	//注销事件
	$("#logout").click(function () {
        $confirm("是否注销?", "您将退出登录,并返回登录页面!", function (isConfirm) {
            if (isConfirm) {
            	ws.close();
            	window.location.href = contextPath+ "/logout";
            }
        }, {confirmButtonText: '确定', cancelButtonText: '取消', width: 400});
    });
	
    // 弹出框配置
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-top-right",
        "showDuration": "500",
        "hideDuration": "5000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }

}